package com.sirenbang.easyshop.controller_front;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;

import com.sirenbang.easyshop.domain.Goods;
import com.sirenbang.easyshop.service.GoodQueryHelper;
import com.sirenbang.easyshop.service.GoodService;
import com.sirenbang.easyshop.service.impl.GoodServiceImpl;
import com.sirenbang.easyshop.utils.Page;

public class SearchServlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public SearchServlet() {
		super();
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
			request.setCharacterEncoding("utf-8");
			this.doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		request.setCharacterEncoding("utf-8");
		String act=request.getParameter("act").trim();
		if("getByKey".equals(act)){
			String key=request.getParameter("key");
			String pageNo = request.getParameter("pageNo");
			System.out.println(key);
			Page page=new Page();
			if(StringUtils.isNotEmpty(pageNo))
				page.setPageNo(Integer.parseInt(pageNo));
			
			GoodQueryHelper helper=new GoodQueryHelper();
			if(StringUtils.isNotEmpty(key))
				helper.setKey(key);
			GoodService service=new GoodServiceImpl();
			try{
				Integer no=Integer.parseInt(key);
			}catch(NumberFormatException e){
				request.setAttribute("good", service.loadGoodByName(key));
				request.setAttribute("key", key);
				request.getRequestDispatcher("jsps/front/customer/good_detail.jsp").forward(request, response);
				return;
			}
			
		}
	}

}
